<?php
namespace V1;

class SharingLog extends \ResourceClass
{
    /**
     * 增加分享日志记录
     */
    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['user_id'])) {
            return '用户ID不能为空';
        }
        $checkRes = $this->autoCheck($req['user_id']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        \Util\LogUtil::log('share','payload:'.$data);
        try {
            $sharing_log = new \Orm\SharingLog\SharingLogClass();
            $sharing_log['user_id'] = $req['user_id'];
            $sharing_log['that_day'] = date('Y-m-d',time());
            if(isset($data['share_goals'])){
                $sharing_log['share_goals'] = $data['share_goals'];
            }
            if(isset($data['title'])){
                $sharing_log['title'] = $data['title'];
            }
            if(isset($data['desc'])){
                $sharing_log['desc'] = $data['desc'];
            }
            if(isset($data['link'])){
                $sharing_log['link'] = $data['link'];
            }
            if(isset($data['imgUrl'])){
                $sharing_log['imgUrl'] = $data['imgUrl'];
            }
            $sharing_log->insert();
            //检查用户剩余竞猜机会
            $thisDate = date('Y-m-d');
            $shareNum = \Biz\SharingLog::getShareByUidAndTime($req['user_id'],$thisDate);
            $result['share_num'] = $shareNum[0]['num']??0;
        }catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}